//
//  NProblemTableViewImagesCell.m
//  nncloud
//
//  Created by gdy on 2018/9/27.
//  Copyright © 2018年 youedata. All rights reserved.
//

#import "NProblemTableViewImagesCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "NProblemCollectionViewImageCell.h"
#import "NSDate+Extension.h"

@interface NProblemTableViewImagesCell()<UICollectionViewDelegate, UICollectionViewDataSource>


@property(nonatomic, strong) UIImageView *iconImageView;
@property(nonatomic, strong) UILabel *nameLb;
@property(nonatomic, strong) UILabel *timeLb;

@property(nonatomic, strong) UIImageView *bgImageView;
@property(nonatomic, strong) UILabel *contentLb;

@property(nonatomic, strong) UICollectionView *mainCollectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@end

#define cellIdentifier    @"NProblemCollectionViewImageCell"
@implementation NProblemTableViewImagesCell

#pragma mark - init
- (UICollectionView *)mainCollectionView
{
    if (!_mainCollectionView)
    {
//        _mainCollectionView = [UICollectionView new];
        _mainCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.layout];
        _mainCollectionView.delegate    = self;
        _mainCollectionView.dataSource  = self;
        _mainCollectionView.pagingEnabled = NO;
//        _mainCollectionView.hidden = YES;
        _mainCollectionView.scrollEnabled = NO;
        _mainCollectionView.showsHorizontalScrollIndicator = NO;
        _mainCollectionView.backgroundColor = [UIColor whiteColor];
        [_mainCollectionView registerClass:[NProblemCollectionViewImageCell class] forCellWithReuseIdentifier:cellIdentifier];
    }
    return _mainCollectionView;
}
- (UICollectionViewFlowLayout*)layout
{
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout alloc]init];
        _layout.itemSize     = CGSizeMake(((ScreenWidth - 40) - (3 * 5)) / 4, 60);
        _layout.minimumLineSpacing = 5;
        _layout.minimumInteritemSpacing = 5;
        _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    }
    return _layout;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = 0;
        self.userInteractionEnabled = YES;
        self.iconImageView = [UIImageView new];
        [self.contentView addSubview:self.iconImageView];
        [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(@10);
            make.size.mas_equalTo(CGSizeMake(30, 30));
            make.top.mas_equalTo(@5);
        }];
        
        self.nameLb = [UILabel new];
        self.nameLb.textColor = NColorFrom16RGB(cNameColor);
        [self.contentView addSubview:self.nameLb];
        [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconImageView.mas_right).mas_offset(@5);
            make.top.equalTo(self.iconImageView);
            make.height.equalTo(self.iconImageView.mas_height);
            make.right.mas_equalTo(@-100);
        }];
        
        self.timeLb = [UILabel new];
        self.timeLb.font = [UIFont systemFontOfSize:14];
        self.timeLb.textColor = NColorFrom16RGB(cTimeColor);
        [self.contentView addSubview:self.timeLb];
        [self.timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.iconImageView);
            make.right.mas_equalTo(@-10);
            make.height.equalTo(self.iconImageView.mas_height);
        }];
        
        
        self.bgImageView = [UIImageView new];
        //        self.bgImageView.backgroundColor = [UIColor grayColor];
        self.bgImageView.userInteractionEnabled = YES;
        [self.contentView addSubview:self.bgImageView];
        [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconImageView.mas_left);
            make.right.mas_equalTo(@-10);
            make.bottom.mas_equalTo(@-10);
            make.top.mas_equalTo(self.iconImageView.mas_bottom).mas_offset(@5);
        }];
        self.bgImageView.image = [[UIImage imageNamed:@"气泡"] resizableImageWithCapInsets:UIEdgeInsetsMake( 10, 10, 5, 5) resizingMode:UIImageResizingModeStretch] ;
        
        self.contentLb = [UILabel new];
        self.contentLb.textColor = NColorFrom16RGB(cContentColor);
        self.contentLb.numberOfLines = 0;
        self.contentLb.font = [UIFont systemFontOfSize:14];
        [self.bgImageView addSubview:self.contentLb];
        [self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bgImageView).mas_offset(@10);
            make.top.mas_equalTo(@10);
            make.right.mas_equalTo(@0);
//            make.height.mas_equalTo(@30);
        }];
        
        [self.bgImageView addSubview:self.mainCollectionView];
        [self.mainCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(@10);
            make.right.mas_equalTo(@-10);
            make.top.equalTo(self.contentLb.mas_bottom).mas_offset(@5);
            make.bottom.mas_equalTo(@-2);
        }];
    }
    return self;
}
- (void)setModel:(NProblemModel *)model
{
    _model = model;
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.questionUserUrl] placeholderImage:[UIImage imageNamed:@"kefu"]];
    self.iconImageView.layer.cornerRadius = 15.f;
    self.iconImageView.layer.masksToBounds = YES;
    self.nameLb.text = model.questionUserName;
    self.timeLb.text = [NSDate timeTransfer:model.questionCreateTime];
    
//    self.contentLb.text = model.questionContent;
    if (![model.questionContent containsString:@"【"]) {
        
        self.contentLb.text = model.questionContent;
        
    }else{
        
        NSArray *arr = [model.questionContent componentsSeparatedByString:@"】"];
        //    NSMutableArray *mutArray = [NSMutableArray arrayWithArray:arr];
        //    [mutArray insertObject:@"\n" atIndex:1];
        YYLog(@"%@",arr);
        NSString *colorString = @"【处理进度：等待用户确认】";
        
        //添加换行 并给文字着色
        
        NSString *contentString = [NSString stringWithFormat:@"%@】%@%@",arr[0],@"\n",arr[1]];
        
        NSMutableAttributedString *priceAttribute = [[NSMutableAttributedString alloc] initWithString:contentString];
        NSRange rang = [contentString rangeOfString:colorString];
        [priceAttribute addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Thonburi" size:14] range:rang];
        [priceAttribute addAttribute:NSForegroundColorAttributeName value:NColorFrom16RGB(0xf97c14) range:rang];
        self.contentLb.attributedText = priceAttribute;
    }

    [self.mainCollectionView reloadData];
    
}
#pragma mark - UICollectionDatasoure && UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.model.attachmentList count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NProblemCollectionViewImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
//    cell.
    NSDictionary *dict = [self.model.attachmentList objectAtIndex:indexPath.row];
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:[dict objectForKey:@"attachmentUrl"]] placeholderImage:[UIImage imageNamed:@"kefu"]];
    return cell;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.didSelectItem) {
        self.didSelectItem(indexPath);
    }
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
